package com.smec.moblie.activity;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.smec.moblie.R;
import com.smec.moblie.bean.DeviceInfoBean;
import com.smec.moblie.control.MainControl;
import com.smec.moblie.crash.ExitAppUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sunyubo on 16/8/16.
 */
public class DeviceInfoActivity extends BaseActivity {
    @BindView(R.id.imageview_back)
    ImageView imageview_back;
    @BindView(R.id.textview_display_name)
    TextView textview_display_name;
    @BindView(R.id.textview_own_user)
    TextView textview_own_user;
    @BindView(R.id.textview_own_date)
    TextView textview_own_date;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;

    private ArrayList<DeviceInfoBean> deviceInfoBeanList = new ArrayList<DeviceInfoBean>();

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_deviceinfo);
    }

    @Override
    protected void initView() {
        ExitAppUtils.getInstance().addActivity(this);

//        PublicControl.setMargins(rlTitle, 0, getStatusBarHeight(DeviceInfoActivity.this), 0, 0);

        ButterKnife.bind(this);
        imageview_back.setOnClickListener(this);
    }

    @Override
    protected void getData() {
        new getDeviceInfoTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }


    /**
     * 获取设备领用人信息
     */
    private class getDeviceInfoTask extends AsyncTask<Void, Void, Void> {
        private boolean isSuccess = true;
        private String deviceId;

        @SuppressLint({"HardwareIds", "MissingPermission"})
        @Override
        protected Void doInBackground(Void... params) {
            TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            deviceId = TelephonyMgr.getDeviceId();//设备id
            Log.i("", "~~~deviceId = " + deviceId);
            try {
                deviceInfoBeanList = MainControl.getDeviceInfo(deviceId);
                isSuccess = true;
            } catch (Exception e) {
                e.printStackTrace();
                isSuccess = false;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (deviceInfoBeanList.size() > 0) {
                DeviceInfoBean deviceInfoBean = deviceInfoBeanList.get(0);
                textview_display_name.setText(deviceInfoBean.getDisplay_name());
                textview_own_date.setText(deviceInfoBean.getOwn_date());
                textview_own_user.setText(deviceInfoBean.getOwn_user());
            } else {
                if (isSuccess) {
                    if (null == deviceId || "".equals(deviceId)) {
                        Toast.makeText(DeviceInfoActivity.this, "未获取到本机设备号，无法获取领用人信息", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(DeviceInfoActivity.this, "未获取到本机领用人信息，请联系主管添加", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(DeviceInfoActivity.this, "数据加载失败", Toast.LENGTH_SHORT).show();
                }

            }
            super.onPostExecute(aVoid);
        }
    }

    @Override
    public void onClick(View v) {
        finish();
    }

}
